package com.example.crm.controller;

import com.example.crm.service.UserService;
import com.example.crm.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/loginCheck")
public class UserLoginCheckController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 1. 获取用户信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 2. 调用service 查询
        UserService userService = new UserServiceImpl();

        try {
            boolean login = userService.login(username, password);
            //判断结果-- 跳转--请求转发
            if (login) {
                System.out.println("登陆成功");
                resp.sendRedirect("index.jsp");
            } else {
                System.out.println("登陆失败");
                resp.sendRedirect("login.jsp");
            }
        } catch (SQLException troubles) {
            troubles.printStackTrace();
        }
    }
}
